//
//  ActivityViewController.m
//  Car
//
//  Created by 何晓文 on 15/9/21.
//  Copyright © 2015年 朱鸿儒. All rights reserved.
//

#import "ActivityViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>


@interface ActivityViewController ()<CLLocationManagerDelegate>
@property (nonatomic, strong)CLLocationManager *manager;
@property (nonatomic,strong) MKMapView *mapView;
@property (nonatomic, strong) CLGeocoder *coder;


@end

@implementation ActivityViewController

- (void)viewDidLoad {
	[super viewDidLoad];
	self.mapView = [[MKMapView alloc] initWithFrame:[UIScreen mainScreen].bounds];
	[self.view addSubview:self.mapView];
	
	[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"location"];
	self.manager.delegate = self;
	//8.0以上需要主动发送请求 在plist文件中添加属性
	if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
		[self.manager requestAlwaysAuthorization];
		NSLog(@"iOS8");
	} else {
		NSLog(@"iOS7");
	}
//	[self.manager startUpdatingLocation];
	self.mapView.userTrackingMode =  MKUserTrackingModeFollow;
	[self.manager startUpdatingLocation];
	self.currentCity = @"shanghai";
	[self chooseCityWithCity:self.currentCity];
	
	
}
//定位失败调用
- (void)chooseCityWithCity:(NSString *)city {

	[self.coder geocodeAddressString:city completionHandler:^(NSArray *placemarks, NSError *error) {

		CLPlacemark *placemark = [placemarks firstObject];

		NSDictionary *detail = placemark.addressDictionary;//[@"FormattedAddressLines"];
		NSLog(@"%@",detail);
		
	}];
	 
}
/**
 *  更新位置时调用
 *
 *  @param manager   <#manager description#>
 *  @param locations <#locations description#>
 */
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
	
	CLLocation *nowLocation = [locations firstObject];
	[self.manager stopUpdatingLocation];
	NSLog(@"%f %f", nowLocation.coordinate.longitude, nowLocation.coordinate.latitude);
	[self getCityCoderWithLocation:nowLocation];

	
}
//根据CLLocation对象获取对应的地标信息
- (void)getCityCoderWithLocation:(CLLocation *)location {
	CLLocation *loc = [[CLLocation alloc] initWithLatitude:(float)location.coordinate.latitude longitude:(float)location.coordinate.longitude];
	NSLog(@"%f", loc.coordinate.longitude);
	[self.coder reverseGeocodeLocation:loc completionHandler:^(NSArray *placemarks, NSError *error) {
		
		for (CLPlacemark *placemark in placemarks) {
			NSLog(@"%@ %@ %f %f", placemark.name, placemark.addressDictionary, placemark.location.coordinate.latitude, placemark.location.coordinate.longitude);
			self.currentCity = placemark.locality;
		}
	}];

}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
	if (status == kCLAuthorizationStatusNotDetermined) {
		NSLog(@"等待授权");
	}else if ((status == kCLAuthorizationStatusAuthorizedAlways) ||
			  status ==kCLAuthorizationStatusAuthorizedWhenInUse){
		NSLog(@"授权成功");
		[self.manager startUpdatingLocation];
	}
	
	
	
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

	return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return 10;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"location" forIndexPath:indexPath];
	
	
	
	return cell;
}

- (CLGeocoder *)coder {
	if (!_coder) {
		_coder = [[CLGeocoder alloc] init];
	}
	return _coder;
}
- (CLLocationManager *)manager {
	if (!_manager) {
		_manager = [[CLLocationManager alloc] init];
	}
	return _manager;
}

@end
